﻿// 1289. 下降路径最小和 II.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <vector>


using namespace std;
/*
https://leetcode.cn/problems/minimum-falling-path-sum-ii/description/

给你一个 n x n 整数矩阵 grid ，请你返回 非零偏移下降路径 数字和的最小值。
非零偏移下降路径 定义为：从 grid 数组中的每一行选择一个数字，且按顺序选出来的数字中，相邻数字不在原数组的同一列。


示例 1：
输入：grid = [[1,2,3],[4,5,6],[7,8,9]]
输出：13
解释：
所有非零偏移下降路径包括：
[1,5,9], [1,5,7], [1,6,7], [1,6,8],
[2,4,8], [2,4,9], [2,6,7], [2,6,8],
[3,4,8], [3,4,9], [3,5,7], [3,5,9]
下降路径中数字和最小的是 [1,5,7] ，所以答案是 13 。

示例 2：
输入：grid = [[7]]
输出：7


提示：
n == grid.length == grid[i].length
1 <= n <= 200
-99 <= grid[i][j] <= 99
*/



//class Solution {
//public:
//    int minFallingPathSum(vector<vector<int>>& grid) {
//        int dp[210][210]; memset(dp, 0x3f, sizeof dp);
//
//        for (int i = 0; i < grid[0].size();i++) {
//            dp[0][i] = grid[0][i];
//        }
//
//        for (int i = 1; i < grid.size(); i++) {
//            for (int j = 0; j < grid[0].size(); j++) {
//                int val = grid[i][j];
//                for (int k = 0; k < grid[0].size(); k++) {
//                    if (j == k) continue;
//                    dp[i][j] = min(dp[i][j], dp[i - 1][k] + val);
//                }
//            }
//        }
//
//        int ans = 0x3f3f3f3f;
//        for (int i = 0; i < grid[0].size(); i++) {
//            ans = min(ans, dp[grid.size() - 1][i]);
//        }
//
//        return ans;
//    }
//};


class Solution {
public:
    int minFallingPathSum(vector<vector<int>>& grid) {
        int dp[210][210];
        memset(dp, 0x3f, sizeof dp);

        int a = 0x3f3f3f3f, b = 0x3f3f3f3f, aj = -1, bj = -1;

        for (int i = 0; i < grid[0].size(); i++) {
            if (grid[0][i] < a) {
                int old = a; int oldj = aj;
                a = grid[0][i]; aj = i;
                if (old < b) { b = old; bj = oldj;}
            }
            else if (grid[0][i] >= a && grid[0][i] < b) {
                b = grid[0][i]; bj = i;
            }
        }
        if (grid.size() == 1) {
            return min(a, b);
        }

        for (int i = 1; i < grid.size(); i++) {
            for (int j = 0; j < grid[0].size(); j++) {
                int val = grid[i][j];
                if (j != aj) { dp[i][j] =  a + val;}
                else {  dp[i][j] = b + val;}
            }
            a = 0x3f3f3f3f, b = 0x3f3f3f3f, aj = -1, bj = -1;
            for (int j = 0; j < grid[0].size(); j++) {
                if (dp[i][j] < a && aj != j) {
                    int old = a; int oldj = aj;
                    a = dp[i][j]; aj = j;
                    if (old < b) { b = old; bj = oldj; }
                }
                else if (dp[i][j] >= a && dp[i][j] < b) {
                    b = dp[i][j]; bj = j;
                }
            }
        }

        return min(a, b);
    }
};


int main()
{
    Solution s;
    vector<vector<int>> v{
        {-73,61,43,-48,-36},
        {3,30,27,57,10},
        {96,-76,84,59,-15},
        {5,-49,76,31,-7},
        {97,91,61,-46,67}
    };
    cout << s.minFallingPathSum(v);
} 